//! 8086 寄存器编码模块
//! 
//! 定义16位寄存器的编码（8086模式）

/// 8086 寄存器编码表
pub struct RegisterEncoding;

impl RegisterEncoding {
    /// 获取16位寄存器编码（8086）
    /// 
    /// 返回寄存器的3位编码（用于ModR/M字节）
    /// 仅支持中文寄存器名称
    pub fn get_16bit_register_code(reg: &str) -> Option<u8> {
        match reg {
            "累加器" => Some(0b000),        // ax
            "计数" => Some(0b001),          // cx
            "数据" => Some(0b010),          // dx
            "基址" => Some(0b011),          // bx
            "栈指针" => Some(0b100),         // sp
            "基址指针" => Some(0b101),        // bp
            "源索引" => Some(0b110),         // si
            "目标索引" => Some(0b111),       // di
            _ => None,
        }
    }

    /// 获取段寄存器编码
    /// 
    /// 返回段寄存器的3位编码（用于ModR/M字节）
    /// 仅支持中文段寄存器名称
    pub fn get_segment_register_code(reg: &str) -> Option<u8> {
        match reg {
            "附加段" => Some(0b000),  // ES
            "代码段" => Some(0b001),  // CS
            "栈段" => Some(0b010),    // SS
            "数据段" => Some(0b011),  // DS
            _ => None,
        }
    }

    /// 检查是否为16位寄存器
    pub fn is_16bit_register(reg: &str) -> bool {
        Self::get_16bit_register_code(reg).is_some()
    }

    /// 检查是否为段寄存器
    pub fn is_segment_register(reg: &str) -> bool {
        Self::get_segment_register_code(reg).is_some()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_get_16bit_register_code() {
        assert_eq!(RegisterEncoding::get_16bit_register_code("累加器"), Some(0b000));
        assert_eq!(RegisterEncoding::get_16bit_register_code("计数"), Some(0b001));
        assert_eq!(RegisterEncoding::get_16bit_register_code("数据"), Some(0b010));
        assert_eq!(RegisterEncoding::get_16bit_register_code("基址"), Some(0b011));
        assert_eq!(RegisterEncoding::get_16bit_register_code("栈指针"), Some(0b100));
        assert_eq!(RegisterEncoding::get_16bit_register_code("基址指针"), Some(0b101));
        assert_eq!(RegisterEncoding::get_16bit_register_code("源索引"), Some(0b110));
        assert_eq!(RegisterEncoding::get_16bit_register_code("目标索引"), Some(0b111));
    }

    #[test]
    fn test_get_16bit_register_code_invalid() {
        assert_eq!(RegisterEncoding::get_16bit_register_code("无效"), None);
        assert_eq!(RegisterEncoding::get_16bit_register_code("ax"), None);
        assert_eq!(RegisterEncoding::get_16bit_register_code(""), None);
    }

    #[test]
    fn test_get_segment_register_code() {
        assert_eq!(RegisterEncoding::get_segment_register_code("附加段"), Some(0b000));
        assert_eq!(RegisterEncoding::get_segment_register_code("代码段"), Some(0b001));
        assert_eq!(RegisterEncoding::get_segment_register_code("栈段"), Some(0b010));
        assert_eq!(RegisterEncoding::get_segment_register_code("数据段"), Some(0b011));
    }

    #[test]
    fn test_is_16bit_register() {
        assert!(RegisterEncoding::is_16bit_register("累加器"));
        assert!(RegisterEncoding::is_16bit_register("基址"));
        assert!(!RegisterEncoding::is_16bit_register("无效"));
    }

    #[test]
    fn test_is_segment_register() {
        assert!(RegisterEncoding::is_segment_register("代码段"));
        assert!(RegisterEncoding::is_segment_register("数据段"));
        assert!(!RegisterEncoding::is_segment_register("累加器"));
    }
}

